package com.example.springbootoauth2jwtclientserver.controller;

import com.nimbusds.jwt.JWT;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {

    @RequestMapping(value = "/userInfo")
    public Map<String,Object> userInfo(Authentication authentication){
        Map<String,Object> map = new HashMap<>();
        Object principal = authentication.getPrincipal();
        if (principal instanceof Jwt) {
            map.put("username",((Jwt)principal).getClaim("user_name"));
            map.putAll(((Jwt)principal).getClaims());
        }
        return map;
    }
}
